package com.gobo.orderservice.service.impl;

import com.gobo.orderservice.client.EduServiceClient;
import com.gobo.orderservice.client.UcenterMemberClient;
import com.gobo.orderservice.pojo.Order;
import com.gobo.orderservice.mapper.OrderMapper;
import com.gobo.orderservice.service.OrderService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gobo.orderservice.utils.OrderNoUtil;
import com.gobo.servicebase.vo.CourseInfoFormVo;
import com.gobo.servicebase.vo.MemberInfoVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;

/**
 * <p>
 * 订单 服务实现类
 * </p>
 *
 * @author gobo-generator
 * @since 2021-09-25
 */
@Service
public class OrderServiceImpl extends ServiceImpl<OrderMapper, Order> implements OrderService {

    @Autowired
    private EduServiceClient eduServiceClient;

    @Autowired
    private UcenterMemberClient memberClient;

    @Override
    public String saveOrder(String courseId, String memberId) {
        //根据课程ID  获取课程详情信息  CourseInfoVo
        CourseInfoFormVo courseInfoFormVo = eduServiceClient.getCommonCourseInfoFormVo(courseId);
        //获取用户信息
        MemberInfoVo memberInfoVo = memberClient.getMemberInfoVo(memberId);
        //创建订单
        Order order = new Order();
        order.setOrderNo(OrderNoUtil.getOrderNo());
        order.setCourseId(courseId);
        order.setCourseTitle(courseInfoFormVo.getTitle());
        order.setCourseCover(courseInfoFormVo.getCover());
        order.setTeacherName(courseInfoFormVo.getTeacherName());


        //TODO  由于采用别人的商户微信收款，故这里付款金额生成时默认调整至0.01
        //order.setTotalFee(courseInfoFormVo.getPrice());
        order.setTotalFee(new BigDecimal(0.01));


        order.setMemberId(memberId);
        order.setMobile(memberInfoVo.getMobile());
        order.setNickname(memberInfoVo.getNickname());
        order.setStatus(0); //订单状态
        order.setPayType(1); //订单支付方式  1位微信  默认只有这一种方式
        int i = baseMapper.insert(order);

        return i==1?order.getOrderNo():null;
    }
}
